﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();
        if (!IsPostBack)
        {
            RadioButtonStu.Checked = true;//首次打开页面，默认选择“学生”
            labelCode.Text = RandomNum(4);//显示4位数的验证码
            Session["UserNo"] = null;//保质期
            Session["UserName"] = null;
        }

    }
    protected void ButtonCode_Click(object sender, EventArgs e)
    {
        labelCode.Text = RandomNum(4);//显示4位数的验证码
        UserCode.Text = "";//将验证码输入的内容清空
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        if (UserCode.Text.Trim() != labelCode.Text)
        {
            LabelError.Text = "*您输入验证码有误（或未输入），请重新输入！";
        }
        else
        {
            if (RadioButtonStu.Checked)//选择学生用户登录
            {
                string mysql = "select * from StudentTb where sNo='" + UserNo.Text.Trim() + "'and sPass='" + UserPass.Text.Trim() + "'  ";//查询学号和密码是否正确
                OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
                OleDbDataReader myReader = mycmd.ExecuteReader();
                if (myReader.Read())
                {
                    Session["UserNo"] = UserNo.Text.Trim();//保存学号
                    Session["UserName"] = myReader[2].ToString();//保存姓名
                    Response.Redirect("student/studentMenu.aspx");//跳转到学生页面
                }
                else
                    LabelError.Text = "*您输入学号或密码有误，请重新输入！";
            }
            else if (RadioButtonTch.Checked)//选择教师用户登录
            {
                string mysql = "select * from TeacherTb where tNo='" + UserNo.Text.Trim() + "'and tPass='" + UserPass.Text.Trim() + "'  ";
                OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
                OleDbDataReader myReader = mycmd.ExecuteReader();
                if (myReader.Read())
                {
                    Session["UserNo"] = UserNo.Text.Trim();//保存学号
                    Session["UserName"] = myReader[2].ToString();//保存姓名
                    Response.Redirect("teacher/teacherMenu.aspx");//跳转到教师页面
                }
                else
                    LabelError.Text = "*您输入工号或密码有误，请重新输入！";
            }
            else//选择管理员用户登录
            {
                string mysql = "select * from ManagerTb where nNo='" + UserNo.Text.Trim() + "'and nPass='" + UserPass.Text.Trim() + "'  ";
                OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
                OleDbDataReader myReader = mycmd.ExecuteReader();
                if (myReader.Read())
                {
                    Session["UserNo"] = UserNo.Text.Trim();//保存学号
                    Session["UserName"] = myReader[2].ToString();//保存姓名
                    Response.Redirect("adminstrator/adminMenu.aspx");//跳转到管理员页面
                }
                else
                    LabelError.Text = "*您输入工号或密码有误，请重新输入！";
            }
        }
    }

    public string RandomNum(int n)
    {
        string strchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,,F,G,H," +
            "I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z," +
            "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
        string[] arry = strchar.Split(',');
        string num = "";
        int temp = -1;
        Random rand = new Random();
        for (int i = 1; i < n + 1; i++)
        {
            if (temp != -1)
                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
            int t = rand.Next(62);
            if (temp != -1 && temp == t)
                return RandomNum(n);
            temp = t;
            num += arry[t];
        }
        return num;
    }

}

